package org.xzx.cloud.ad.advice;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.xzx.cloud.ad.exception.AdException;
import org.xzx.cloud.ad.vo.CommonResponse;

import javax.servlet.http.HttpServletRequest;

/**
 * 捕获异常，统一处理
 *
 * @author xzx
 */
@RestControllerAdvice
public class GlobalExceptionAdvice {
    /**
     * 统一处理 {@link AdException} 异常
     *
     * @param request {@link HttpServletRequest}
     * @param e       {@link AdException}
     * @return response
     */
    @ExceptionHandler(value = AdException.class)
    public CommonResponse<String> handlerAdException(HttpServletRequest request, AdException e) {
        CommonResponse<String> response = new CommonResponse<>(-1, "business error!");
        response.setData(e.getMessage());
        return response;
    }
}
